package com.lib.shimmer;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Shimmer User: romainpiel Date: 06/03/2014 Time: 10:19
 *
 * Shimmering TextView Dumb class wrapping a ShimmerViewHelper
 */
public class ShimmerTextView extends TextView implements ShimmerViewBase {

	private ShimmerViewHelper shimmerViewHelper;
	private int primaryColor, reflectionColor;

	public ShimmerTextView(Context context) {
		super(context);
		shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), null);
		shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
	}

	public ShimmerTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
		shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), attrs);
		shimmerViewHelper.setPrimaryColor(primaryColor);
		shimmerViewHelper.setReflectionColor(reflectionColor);
	}

	public ShimmerTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
		shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), attrs);
		shimmerViewHelper.setPrimaryColor(primaryColor);
		shimmerViewHelper.setReflectionColor(reflectionColor);
	}

	public void init(Context context, AttributeSet attrs) {
		TypedArray a = context.obtainStyledAttributes(attrs,
					R.styleable.ShimmerColor);
		primaryColor = a.getColor(R.styleable.ShimmerColor_primaryColor,context.getResources().getColor(R.color.black));
		reflectionColor = a.getColor(R.styleable.ShimmerColor_refColor, context.getResources().getColor(R.color.white));
		a.recycle();
	}

	@Override
	public float getGradientX() {
		return shimmerViewHelper.getGradientX();
	}

	@Override
	public void setGradientX(float gradientX) {
		shimmerViewHelper.setGradientX(gradientX);
	}

	@Override
	public boolean isShimmering() {
		return shimmerViewHelper.isShimmering();
	}

	@Override
	public void setShimmering(boolean isShimmering) {
		shimmerViewHelper.setShimmering(isShimmering);
	}

	@Override
	public boolean isSetUp() {
		return shimmerViewHelper.isSetUp();
	}

	@Override
	public void setAnimationSetupCallback(
			ShimmerViewHelper.AnimationSetupCallback callback) {
		shimmerViewHelper.setAnimationSetupCallback(callback);
	}

	@Override
	public int getPrimaryColor() {
		return shimmerViewHelper.getPrimaryColor();
	}

	@Override
	public void setPrimaryColor(int primaryColor) {
		shimmerViewHelper.setPrimaryColor(primaryColor);
	}

	@Override
	public int getReflectionColor() {
		return shimmerViewHelper.getReflectionColor();
	}

	@Override
	public void setReflectionColor(int reflectionColor) {
		shimmerViewHelper.setReflectionColor(reflectionColor);
	}

	@Override
	public void setTextColor(int color) {
		super.setTextColor(color);
		if (shimmerViewHelper != null) {
			shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
		}
	}

	@Override
	public void setTextColor(ColorStateList colors) {
		super.setTextColor(colors);
		if (shimmerViewHelper != null) {
			shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if (shimmerViewHelper != null) {
			shimmerViewHelper.onSizeChanged();
		}
	}

	@SuppressLint("WrongCall")
	@Override
	public void onDraw(Canvas canvas) {
		if (shimmerViewHelper != null) {
			shimmerViewHelper.onDraw();
		}
		super.onDraw(canvas);
	}
}
